package com.winner.crmsoftware.addcustomer;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import com.winner.crmsoftware.customers.ListCustomerActivity;
import com.winner.crmsoftware.location.GPSTracker;
import com.winner.crmsoftware.main.R;
import com.winner.srmsoftware.json.JSONParser;
import com.winner.srmsoftware.json.ServiceHandler;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AddCustomerActivity extends Activity {

	EditText edt_name;
	EditText edt_address;
	TextView tv_latitude;
	TextView tv_longtitude;
	EditText edt_phone;
	EditText edt_email;

	JSONParser jsonParser = new JSONParser();
	public static final String URL_ADD_CUSTOMER_STRING = "http://103.28.37.16:8080/crm_software/add_new_customer.php";

	private ProgressDialog pDialog;
	GPSTracker gpsTracker;

	Button btn_submit;
	double lati;
	double longti;
	private static final String TAG_SUCCESS = "success";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_customer);
		edt_name = (EditText) findViewById(R.id.edt_name);
		edt_email = (EditText) findViewById(R.id.edt_email);
		edt_address = (EditText) findViewById(R.id.edt_address);
		edt_phone = (EditText) findViewById(R.id.edt_phone);
		btn_submit = (Button) findViewById(R.id.btn_add);

		tv_latitude = (TextView) findViewById(R.id.tv_latitude);
		tv_longtitude = (TextView) findViewById(R.id.tv_longtitude);
		btn_submit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				new AddNewCustomer().execute();
			}
		});

		gpsTracker = new GPSTracker(AddCustomerActivity.this);
		if (!gpsTracker.canGetLocation()) {
			gpsTracker.showSettingsAlert();
		}

		new GetLocation().execute();

	}

	private class GetLocation extends AsyncTask<Void, Void, Void> {

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			// Showing progress dialog
			pDialog = new ProgressDialog(AddCustomerActivity.this);
			pDialog.setMessage("Please wait...");
			pDialog.setCancelable(false);
			pDialog.show();

		}

		@SuppressLint("DefaultLocale")
		@Override
		protected Void doInBackground(Void... arg0) {
			// Creating service handler class instance
			lati = gpsTracker.getLatitude();
			longti = gpsTracker.getLongitude();

			// Making a request to url and getting response
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			// Dismiss the progress dialog
			if (pDialog.isShowing())
				pDialog.dismiss();
			/**
			 * Updating parsed JSON data into ListView
			 * */
			tv_latitude.setText(String.valueOf(lati));
			tv_longtitude.setText(String.valueOf(longti));
		}
	}

	/**
	 * Background Async Task to Create new product
	 * */
	class AddNewCustomer extends AsyncTask<String, String, String> {

		/**
		 * Before starting background thread Show Progress Dialog
		 * */
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pDialog = new ProgressDialog(AddCustomerActivity.this);
			pDialog.setMessage("Creating Product..");
			pDialog.setIndeterminate(false);
			pDialog.setCancelable(true);
			pDialog.show();
		}

		/**
		 * Creating product
		 * */
		protected String doInBackground(String... args) {
			
			String name = edt_name.getText().toString();
			String address = edt_address.getText().toString();
			String latitude = tv_latitude.getText().toString();
			String longtitude = tv_longtitude.getText().toString();
			String phone_number = edt_phone.getText().toString();
			String email = edt_email.getText().toString();

			// Building Parameters
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("name", name));
			params.add(new BasicNameValuePair("address", address));
			params.add(new BasicNameValuePair("latitude", latitude));
			params.add(new BasicNameValuePair("longtitude", longtitude));
			params.add(new BasicNameValuePair("phone_number", phone_number));
			params.add(new BasicNameValuePair("email", email));
			

			// getting JSON Object
			// Note that create product url accepts POST method
			JSONObject json = jsonParser.makeHttpRequest(
					URL_ADD_CUSTOMER_STRING, "POST", params);

			try {
				int success = json.getInt(TAG_SUCCESS);

				if (success == 1) {
					// successfully created product
					Intent i = new Intent(getApplicationContext(),
							ListCustomerActivity.class);
					startActivity(i);

					// closing this screen
					finish();
				} else {
					// failed to create product
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}

			return null;
		}

		/**
		 * After completing background task Dismiss the progress dialog
		 * **/
		protected void onPostExecute(String file_url) {
			// dismiss the dialog once done
			pDialog.dismiss();
		}

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

}
